﻿namespace NTLib.IOC.Containers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TInstance">The type of the instance.</typeparam>
    /// <seealso cref="NTLib.IOC.Containers.TypeContainer" />
    public class ProviderContainer<TInstance> : TypeContainer
    {
        #region Fields

        private readonly Func<object, CancellationToken, TInstance> _provider;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="ProviderContainer{TInstance}"/> class.
        /// </summary>
        public ProviderContainer(Func<object, CancellationToken, TInstance> provider)
            : base(typeof(TInstance), typeof(TInstance), false)
        {
            this._provider = provider;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the specified state.
        /// </summary>
        /// <typeparam name="TInstanceAsk">The type of the instance ask.</typeparam>
        /// <param name="state">The state.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public override TInstanceAsk Get<TInstanceAsk>(object state = null, CancellationToken token = default(CancellationToken))
        {
            return (TInstanceAsk)(object)this._provider(state, token);
        }

        #endregion
    }
}
